package com.sjsu.cmpe239.evaluationTests;

import java.io.File;
import java.io.IOException;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.common.Weighting;
import org.apache.mahout.cf.taste.eval.IRStatistics;
import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
import org.apache.mahout.cf.taste.eval.RecommenderEvaluator;
import org.apache.mahout.cf.taste.eval.RecommenderIRStatsEvaluator;
import org.apache.mahout.cf.taste.impl.eval.AverageAbsoluteDifferenceRecommenderEvaluator;
import org.apache.mahout.cf.taste.impl.eval.GenericRecommenderIRStatsEvaluator;
import org.apache.mahout.cf.taste.impl.eval.RMSRecommenderEvaluator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.model.jdbc.AbstractJDBCDataModel;
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel;
import org.apache.mahout.cf.taste.impl.recommender.ClusterSimilarity;
import org.apache.mahout.cf.taste.impl.recommender.FarthestNeighborClusterSimilarity;
import org.apache.mahout.cf.taste.impl.recommender.TreeClusteringRecommender;
import org.apache.mahout.cf.taste.impl.recommender.slopeone.SlopeOneRecommender;
import org.apache.mahout.cf.taste.impl.recommender.slopeone.jdbc.MySQLJDBCDiffStorage;
import org.apache.mahout.cf.taste.impl.recommender.svd.ALSWRFactorizer;
import org.apache.mahout.cf.taste.impl.recommender.svd.SVDRecommender;
import org.apache.mahout.cf.taste.impl.similarity.LogLikelihoodSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.model.JDBCDataModel;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.recommender.slopeone.DiffStorage;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.junit.BeforeClass;
import org.junit.Test;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class ClusterBasedRecommenderEvalatorTest {

	static FileDataModel model;

	@BeforeClass
	public static void buildDataModel() throws IOException {
		MysqlDataSource dataSource = new MysqlConnectionPoolDataSource();
		dataSource.setServerName("localhost");
		dataSource.setUser("root");
		dataSource.setPassword("");
		dataSource.setDatabaseName("239db");

		model = new FileDataModel(new File("d:/ratings.csv"));

	}


	@Test
	public void evaluateClusterBasedRecommemder() throws TasteException {

		// Start: evaluate with Average absolute difference
		RecommenderEvaluator evaluator = new AverageAbsoluteDifferenceRecommenderEvaluator();
		RecommenderBuilder builder = new RecommenderBuilder() {
			@Override
			public Recommender buildRecommender(DataModel model)
					throws TasteException {

				UserSimilarity similarity = new LogLikelihoodSimilarity(model);
				ClusterSimilarity clusterSimilarity =
				new FarthestNeighborClusterSimilarity(similarity);
				return new TreeClusteringRecommender(model, clusterSimilarity, 10);

			}
		};

		double score = evaluator.evaluate(builder, null, model, 0.7, 1.0);
		System.out.println("\n=======evaluateClusterBasedRecommemder");
		System.out
				.println("AverageAbsoluteDifferenceEvaluator score: " + score);

		// Start: evaluate with RMSRecommenderEvaluator
		evaluator = new RMSRecommenderEvaluator();
		score = evaluator.evaluate(builder, null, model, 0.7, 1.0);
		System.out.println("RMSEvaluator score: " + score);

		
//		  RecommenderIRStatsEvaluator irStatsEvaluator = new
//		  GenericRecommenderIRStatsEvaluator(); 
//		  IRStatistics stats =
//		  irStatsEvaluator.evaluate(builder, null, model, null, 2, 1.0, 1.0);
//		  System.out.println("IRStatistics precision: " +
//		  stats.getPrecision()); System.out.println("IRStatistics recall: " +
//		  stats.getRecall());

	}

	
}
